package com.ddd.test.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ddd.test.dto.cmd.OrderDTO;
import com.ddd.test.dto.cmd.SalesDTO;
import com.ddd.test.result.Result;
import com.ddd.test.service.SalesService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * SalesRestService服务接口，REST方式
 *
 * 
 */
@RestController
@RequestMapping("/sales")
public class SalesRestService {

   @Autowired
   SalesService salesService;
  /**
   * 按code禁用
   *
   * @return Result<String>
   */

  @RequestMapping(value = "/disable", method = {RequestMethod.POST})
  Result<String> disable(@RequestBody String code){
	  return salesService.disable(code);
  };

  /**
   * 新增
   *
   * @param record 记录
   * @return Result<String>
   */

  @PostMapping(value = "/create")
  Result<String> create(@RequestBody SalesDTO record){
	  return salesService.create(record);
  }
  


  /**
   * 按code查询
   *
   * @return Result<SalesDTO>
   */

  @RequestMapping(value = "/getSales", method = {RequestMethod.POST})
  Result<SalesDTO> getSales(@RequestBody String code) {
	  return salesService.getSales(code);
  };


  /**
   * 调级
   *
   * @return Result<String>
   */

  @PostMapping(value = "/changeLevel")
  Result<Integer> changeLevel(@RequestBody String param){
	  String currentSales= "001"; //TODO 当前登录用户	  
	  JSONObject object= JSON.parseObject(param);
	  Integer level = object.getInteger("level");
	  return salesService.changeLevel(level,currentSales);
  };

  /**
   * 新人带客户资源入职
   *
   * @return Result<String>
   */

  @PostMapping(value = "/entryAndOrders")
  Result<String> entryAndOrders(@RequestBody Param param){
	  return salesService.entryAndOrders(param.sales,param.order);
  };
}

class Param{
	  public SalesDTO sales;
	  public OrderDTO order;  
}